<?php
namespace app\api\controller;

use app\common\controller\IndexBaseController;
use app\web\model\AgentModel;
use app\web\model\AddressModel;
use app\web\model\OrderItemsModel;
use app\web\model\OrderModel;
use app\admin\model\Member as MemberModel;
use think\Db;
/**
 * 商品处理
 */
class IndexController extends IndexBaseController {

	protected $discountGoods_model;
	protected $grabGoods_model;
	protected $pintuanGoods_model;
	protected $scoreGoods_model;
	protected $agent_model;
	protected $member_model;
	protected $address_model;
	

	protected function _initialize() {
		parent::_initialize();
		$this->agent_model = new AgentModel();
		$this->member_model = new MemberModel();
		$this->address_model = new AddressModel();
	}
	

	/**
	 * 
	 * 获取单个商品信息
	 */
	 public function getGoods(){
		$goodsType = request() -> param('goods_type');
		$goods_id = request() -> param('goods_id');
		$goodsTable = GoodsController::getNumType($goodsType);
		$cut_goods = Db::name($goodsTable)->where('id', $goods_id)->find();
		if ($cut_goods) {
			$cut_goods['agent_address'] = $this->agent_model->where('id',$cut_goods['agent_id'])->value('address');
			$cut_goods['content'] = html_entity_decode($cut_goods['content']);
			$cut_goods['goodsType'] = $goodsType;
			if ($cut_goods) {
				return $this -> toJson(200, '', $cut_goods);
			} else {
				return $this -> toJson(405, '数据不存在');
			}
		} else {
			return $this -> toJson(402, '参数不能为空');
		}
		
	 }
	 
	 /**
	  * 获取商品列表
	  */
	public function getGoodsList($page = 1){
	$goodsType = request() -> param('goods_type');
		$longitude = request() -> param('longitude');
		$latitude = request() -> param('latitude');
	   $a= request() -> param('a');
	   $cishu= request() -> param('cishu');
	   $where=[];
	   $map=[];
	   if($goodsType==1) {
	     $goodsTable ='long_mall_goods_grab';
	    	 $a=input('tel');
			if($a!=''){
					$where['name'] = ['like', '%'.$a.'%'];
					$map['name'] =$a;	
					$list2=Db::name('mall_search')->where($map)->find();
			   if($list2){
			   		$count=$list2['count']+1;
		   	                   Db::name('mall_search')->where('id',$list2['id'])->update(['count'=>$count]);			 		
		   	        }else{
			 				   Db::name('mall_search')->insert(['name'=>$a,'count'=>1,'statue'=>$goodsType]);
			 		}
				}
	         }
	      elseif($goodsType==2) {
		   $goodsTable='long_mall_goods_discount';
		    $a=input('tel');
			if($a!=''){
					$where['name'] = ['like', '%'.$a.'%'];
					$map['name'] =$a;	
					$list2=Db::name('mall_search')->where($map)->find();
			   if($list2){
			   		$count=$list2['count']+1;
		   	                   Db::name('mall_search')->where('id',$list2['id'])->update(['count'=>$count]);			 		
		   	        }else{
			 				   Db::name('mall_search')->insert(['name'=>$a,'count'=>1,'statue'=>$goodsType]);
			 		}
				}
      }
	       elseif($goodsType==3) {
		   $goodsTable='long_mall_goods_score';
		    $a=input('tel');
			if($a!=''){
					$where['name'] = ['like', '%'.$a.'%'];
					$map['name'] =$a;	
					$list2=Db::name('mall_search')->where($map)->find();
			   if($list2){
			   		$count=$list2['count']+1;
		   	                   Db::name('mall_search')->where('id',$list2['id'])->update(['count'=>$count]);			 		
		   	        }else{
			 				   Db::name('mall_search')->insert(['name'=>$a,'count'=>1,'statue'=>$goodsType]);
			 		}
				}
	     }
	$sql = 'select id,thumb,name,sales,original_price,current_price,need_score,longitude,latitude,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(('.$latitude.'*PI()/180-latitude*PI()/180)/2),2)+COS('.$latitude.'*PI()/180)*COS(latitude*PI()/180)*POW(SIN(('.$longitude.'*PI()/180-longitude*PI()/180)/2),2)))*1000) AS juli from '.$goodsTable.'';
    $goodsList=Db::query($sql);
			
		//按时间升降序查询
	if($a=='x'){
   if($cishu%2==0){
	   $sql = 'select id,thumb,name,sales,original_price,current_price,need_score,longitude,latitude,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(('.$latitude.'*PI()/180-latitude*PI()/180)/2),2)+COS('.$latitude.'*PI()/180)*COS(latitude*PI()/180)*POW(SIN(('.$longitude.'*PI()/180-longitude*PI()/180)/2),2)))*1000) AS juli from '.$goodsTable.'  order by add_time desc';
      $goodsList=Db::query($sql);
   }elseif($cishu%2==1){
       $sql = 'select id,thumb,name,sales,original_price,current_price,need_score,longitude,latitude,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(('.$latitude.'*PI()/180-latitude*PI()/180)/2),2)+COS('.$latitude.'*PI()/180)*COS(latitude*PI()/180)*POW(SIN(('.$longitude.'*PI()/180-longitude*PI()/180)/2),2)))*1000) AS juli from '.$goodsTable.'  order by add_time asc';
    $goodsList=Db::query($sql);
	   }
	}
	//按价格升降序查询
	if($a=='z'){
	if($cishu%2==0){
	$sql = 'select id,thumb,name,sales,original_price,current_price,need_score,longitude,latitude,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(('.$latitude.'*PI()/180-latitude*PI()/180)/2),2)+COS('.$latitude.'*PI()/180)*COS(latitude*PI()/180)*POW(SIN(('.$longitude.'*PI()/180-longitude*PI()/180)/2),2)))*1000) AS juli from '.$goodsTable.' order by current_price desc';
    $goodsList=Db::query($sql);
	}elseif($cishu%2==1){
	    $sql = 'select id,thumb,name,sales,original_price,current_price,need_score,longitude,latitude,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(('.$latitude.'*PI()/180-latitude*PI()/180)/2),2)+COS('.$latitude.'*PI()/180)*COS(latitude*PI()/180)*POW(SIN(('.$longitude.'*PI()/180-longitude*PI()/180)/2),2)))*1000) AS juli from '.$goodsTable.'  order by current_price  asc';
    $goodsList=Db::query($sql);
	  }
	} 
	
	//按距离升降序查询
	if($a=='y'){
	if($cishu%2==0){
	$sql = 'select id,thumb,name,sales,original_price,current_price,need_score,longitude,latitude,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(('.$latitude.'*PI()/180-latitude*PI()/180)/2),2)+COS('.$latitude.'*PI()/180)*COS(latitude*PI()/180)*POW(SIN(('.$longitude.'*PI()/180-longitude*PI()/180)/2),2)))*1000) AS juli from '.$goodsTable.'  order by juli desc';
    $goodsList=Db::query($sql);
	}elseif($cishu%2==1){
	    $sql = 'select id,thumb,name,sales,original_price,current_price,need_score,longitude,latitude,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(('.$latitude.'*PI()/180-latitude*PI()/180)/2),2)+COS('.$latitude.'*PI()/180)*COS(latitude*PI()/180)*POW(SIN(('.$longitude.'*PI()/180-longitude*PI()/180)/2),2)))*1000) AS juli from '.$goodsTable.'  order by juli  asc';
      $goodsList=Db::query($sql);
	  }
	} 
	if(input('tel')!=''){
		$field="id,thumb,name,sales,original_price,current_price,need_score,longitude,latitude,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(('.$latitude.'*PI()/180-latitude*PI()/180)/2),2)+COS('.$latitude.'*PI()/180)*COS(latitude*PI()/180)*POW(SIN(('.$longitude.'*PI()/180-longitude*PI()/180)/2),2)))*1000) AS juli ";
		$goodsList=Db::table($goodsTable)->field($field)->where($where)->select();
	}

			return $this -> toJson(200, '', $goodsList);
	}

	 /**
	 * 获取秒杀商品信息
	 */	
	public function kill(){
			$goodsList = Db::name("mall_goods_discount")->select();
				return $this -> toJson(200, '',$goodsList);
		
	}
	 /**
	 * 获取附近商家
	 */
	public function nearbyseller(){
		$id = request() -> param('id');
		$json=Db::name('mall_agent')
	 	->where('industry_id='.$id)
	 	->select();
	return $this -> toJson(200, '',$json);

	}

		 /**
	  * 获取云拼团商品列表
	  */
	public function listyun(){
		$where=[];
		$map=[];
			$a=input('tel');
			if($a!=''){
					$where['name'] = ['like', '%'.$a.'%'];
					$map['name'] =$a;	 
					$list2=Db::name('mall_search')->where($map)->find();
			   if($list2){
			   		$count=$list2['count']+1;
		   	                   Db::name('mall_search')->where('id',$list2['id'])->update(['count'=>$count]);			 		
		   	        }else{
			 				   Db::name('mall_search')->insert(['name'=>$a,'count'=>1,'statue'=>4]);
			 		}
				}
		$goodsList = Db::name('mall_goods_pintuan')->where($where)->select();
		if(count($goodsList)>0){
			return $this -> toJson(200, '', $goodsList);
		}else{
			return $this -> toJson(405, '数据异常');
		}
	}
	 /**
	  * 获取云拼团商品详情
	  */
	public function Tshow(){
		$id=input('id');
		$where['a.pid']=0;
		$where['a.goods_id']=$id;
		$where['a.status']=1;
		$list1=Db::name('mall_goods_pintuan')->alias('a')
		->join('mall_agent b','a.agent_id=b.id','LEFT')->field('a.*,b.address')
		->where('a.id',$id)->find();
		$data=Db::name('mall_join_pintuan')->alias('a')
		->join('member b','a.uid=b.id','LEFT')
		->field('a.*,b.thumb')
		->where($where)->limit(3)->order('add_time desc')->select();
		$list3=Db::name('mall_join_pintuan')->field('pid,uid')->where('uid',input('uid'))->select();		
		$list[]=$list1;
		$list[]=$data;
		$list[]=$list3;
		//var_dump($list);
		return $this -> toJson(200, '', $list);
	}
	
	
	/**
	  * 用户开团
	  */
	public function ktuan(){
		$uid=input('uid');
		$goods_id=input('id');
		$list1=Db::name('mall_goods_pintuan')->where('id',$goods_id)->find();
		$list2=Db::name('member')->where('id',$uid)->find();
		$data=['uid'=>$uid,'uname'=>$list2['real_name'],'goods_id'=>$goods_id,'add_time'=>input('time'),'goods_name'=>$list1['name'],];
		if(Db::name('mall_join_pintuan')->insert($data)){
			return $this -> toJson(200, '', 'ok');
		}
	}
	//参团详情
	
	public function ctshow(){
		$list=Db::name('mall_join_pintuan')->alias('a')
		->join('mall_goods_pintuan b','a.goods_id=b.id','LEFT')
		->join('member c','c.id=a.uid','LEFT')
		->field('b.name,b.current_price,b.thumb goods_thumb,b.need,b.sales,a.goods_price,a.add_time,c.real_name,c.thumb')
		->where('a.id',input('pid'))
		->find();
		$list1=Db::name('mall_join_pintuan')->alias('a')
		->join('member c','c.id=a.uid','LEFT')
		->field('c.thumb,c.real_name')
		->where('a.pid',input('pid'))->select();
		$data[]=$list;
		$data[]=$list1;
		$sql['pid']=input('pid');
		$sql['uid']=input('uid');
		$list3=Db::name('mall_join_pintuan')->where($sql)->find();
		//var_dump($data);
		if($list3){
			$data[]=1;
		}else{
			$data[]=2;
		}
		return $this -> toJson(200, '', $data);
	}
	
	
	//用户参团
	public function ctuan(){
		$uid=input('uid');
		$goods_id=input('id');
		$need=input('need')+1;
		$list1=Db::name('mall_goods_pintuan')->where('id',$goods_id)->find();
		$list2=Db::name('member')->where('id',$uid)->find();
		if($need==$list1['need']){
			Db::name('mall_join_pintuan')->where('id', input('pid'))->update(['need' =>$need,'status'=>'2']);
		}
		Db::name('mall_join_pintuan')->where('id', input('pid'))->update(['need' =>$need]);
		$data=['uid'=>$uid,'uname'=>$list2['real_name'],'goods_id'=>$goods_id,'add_time'=>input('time'),'goods_name'=>$list1['name'],'pid'=>input('pid')];
		if(Db::name('mall_join_pintuan')->insert($data)){
			return $this -> toJson(200, '', $need);
		}
	}
	
	//人数没满时间停止
	public function stoptime(){
		if(input('time')==1){
			Db::name('mall_join_pintuan')->where('id', input('pid'))->update(['status' =>3]);
			return $this -> toJson(200, '', 'ok');
		}
		
	}
	
	
	//秒杀
	public function seckill(){
				if(input('id')){
					$goodsList = Db::name("mall_goods_discount")->where('id',input('id'))->find();
					return $this -> toJson(200, '',$goodsList);
				}else{
					//$where=[];
					//$where['limit1']='1';
					$goodsList = Db::name("mall_goods_discount")->where('limit1','=','1')->select();
			//var_dump($goodsList);
				return $this -> toJson(200, '',$goodsList);

				}
	}
		
		//查询记录
	public function search(){
		$goodsType = request() -> param('goods_type');
	    $where=[];
	    if($goodsType==1){
			$where['statue']=1;
		}
		if($goodsType==2){
			$where['statue']=2;
		}
		if($goodsType==3){
			$where['statue']=3;
		}
		if($goodsType==4){
			$where['statue']=4;
		}
		$list=Db::name('mall_search')->where($where)->order('count desc')->limit(6)->select();
		return $this -> toJson(200, '', $list);
	}
		
	/**
	 * 综合首页附近商户
	 */
	function getStoreList(){
		$longitude = request() -> param('longitude');
		$latitude = request() -> param('latitude');
				
		$where = ' order by juli asc ';
		$sql = 'select agent_id,agent_name,thumb,address,business_scope,longitude,latitude,template_id,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(('.$latitude.'*PI()/180-latitude*PI()/180)/2),2)+COS('.$latitude.'*PI()/180)*COS(latitude*PI()/180)*POW(SIN(('.$longitude.'*PI()/180-longitude*PI()/180)/2),2)))*1000) AS juli from long_mall_location'.$where.' limit 0,3';
		
		$resultList=Db::query($sql);
		
		if($resultList){
			return $this->toJson(200, 'ok', $resultList);
		}else{
			return $this->toJson(405, '',"数据异常");
		}
	}
	/**
	 * 商品列表信息
	 */
	function get_goods_list($page = 1){
		$goods_type = request() -> param('goods_type');
		$order_by = request() -> param('order_by');
		$sort_type = request() -> param('sort_type');
		$search_goods = request() -> param('search_goods');
		$longitude = request() -> param('longitude');
		$latitude = request() -> param('latitude');
		
		
		$next_page =  intval(input('next_page')) ? intval(input('next_page')) : 1;
		
		$goodsTable = $this->getNumType($goods_type);
		
		$newgoodsTable = 'long_'.$goodsTable;

		if(strlen($search_goods)>1){
			$where = ' where name like "%'.$search_goods.'%"';
		}else{
			$where = '';
		}
		
		//获取总记录数
		$sql2 = 'select count(id) as total_num  from '.$newgoodsTable.$where;
		$total_num = Db::query($sql2);
		
		$where .= ' order by '.$order_by.' '.$sort_type;
		
		$sql = 'select id,thumb,name,sales,original_price,current_price,need_score,give_score,longitude,latitude,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(('.$latitude.'*PI()/180-latitude*PI()/180)/2),2)+COS('.$latitude.'*PI()/180)*COS(latitude*PI()/180)*POW(SIN(('.$longitude.'*PI()/180-longitude*PI()/180)/2),2)))*1000) AS juli from '.$newgoodsTable.$where.' limit '.(($next_page-1)*4).',4';
		$goodsList=Db::query($sql);
		
		if($goodsList){
			return $this->toJson(200, $total_num[0]['total_num'], $goodsList);
		}else{
			return $this->toJson(405, '',"数据异常");
		}
	}
	/**
	 * 评价列表信息
	 */
	function get_comment_list($page = 1){
		$where['a.goods_id']=request() -> param('goods_id');
		$where['a.agent_status']=1;
		$comment_list=Db::name('mall_comment')->alias('a')->join('member m','a.uid=m.id')->where($where)->order('a.add_time desc')->limit(6)->select();
		//$a=db('mall_comment')->getlastsql();
		if($comment_list){
			return $this->toJson(200, 'ok', $comment_list);
		}else{
			return $this->toJson(405, '0','数据异常');
		}
	}
	
	/**
	 * 获取商品类型
	 */
	 public function getNumType($num){
	 	switch ($num)
		{
		case 2:
		  return "mall_goods_discount";
		  break;  
		case 4:
		   return "mall_goods_pintuan";
		  break;
		case 3:
		   return "mall_goods_score";
		  break;
		case 1:
		   return "mall_goods_grab";
		  break;
		default:
			return null;
		 break;
		}
	}
}
